package br.unifor.mia.graph.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import br.unifor.mia.graph.setorcensitario.Node;

public class DB {

	private static Session session;

	static {
		Configuration cfg = new Configuration();
		cfg.configure();
		SessionFactory factory = cfg.buildSessionFactory();
		session = factory.openSession();
	}

	public static void importData() {

		try {
			File file = new File(new File(".").getCanonicalPath()+ "\\resources\\usuarios_por_setor_temporal.txt");
			FileReader fileReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fileReader);

			ImportTextToDB.populate(reader, getSession());

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Session getSession() {
		return session;
	}

	@SuppressWarnings("unchecked")
	public static List<Node> getUserNodeList() {
		return DB.getSession().createQuery("select n from Node n where n.name like 'User%'").list();
	}

	@SuppressWarnings("unchecked")
	public static List<Node> getSectorNodeList() {
		return DB.getSession().createQuery("select n from Node n where n.name like 'Sector%'").list();
	}
}
